/*

* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Smart-Shop开源商城系统/ Java商城-首选电商平台系统 请务必保留此注释头信息
* |     开源地址: https://gitee.com/ningbo-qishan/gruul-mall
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     可正常分享和学习源码,未经授权 不可商用！
* |     商业版中台购买/开源版商业授权 联系技术客服
* |     官网:  https://www.73app.cn/
* ------------------------------------------------------------------------
* ------------------------------------------------------------------------
* |     Copyright (c) 2016-2999 宁波启山智软科技有限公司
* |     版权所有 ,侵权必究！
* ------------------------------------------------------------------------

*/
package com.medusa.gruul.integral.task;

import cn.hutool.core.collection.CollectionUtil;
import com.medusa.gruul.integral.api.entity.IntegralRule;
import com.medusa.gruul.integral.api.entity.IntegralShop;
import com.medusa.gruul.integral.mapper.IntegralRuleMapper;
import com.medusa.gruul.integral.mapper.IntegralShopMapper;
import com.medusa.gruul.integral.service.IIntegralDetailService;
import com.xxl.job.core.biz.model.ReturnT;
import com.xxl.job.core.handler.IJobHandler;
import com.xxl.job.core.handler.annotation.JobHandler;
import lombok.extern.slf4j.Slf4j;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.text.ParseException;
import java.time.LocalDateTime;
import java.util.List;
import java.util.Map;

/**
 * @Description: 定期清空积分
 * @Author: whh
 * @Date : 2020/1/10 9:07
 */
@JobHandler(value = "ClearIntegralJob")
@Component
@Slf4j
public class ClearIntegralJob extends IJobHandler {

	private final Logger logger = LoggerFactory.getLogger(getClass());

	@Autowired
	private IntegralShopMapper integralShopMapper;

	@Autowired
	private IntegralRuleMapper integralRuleMapper;

	@Autowired
	private IIntegralDetailService integralDetailService;

	@Override
	public ReturnT<String> execute(String s) throws Exception {
		Integer validity = 0;
		String shopId = "";
		String tenantId = "";
		List<IntegralShop> integralShops = integralShopMapper.selectAllShopInfo();
		if (integralShops.size() > 0) {
			for (IntegralShop integralShop : integralShops) {
				List<IntegralRule> rules = integralRuleMapper.getRules(integralShop.getId());
				if (CollectionUtil.isNotEmpty(rules)) {
					for (IntegralRule rule : rules) {
						validity = rule.getIntegralValidity();
						shopId = rule.getShopId();
						tenantId = rule.getTenantId();
						if (validity != null & shopId != null & tenantId != null) {
							break;
						}
					}
					LocalDateTime createTime = integralShop.getCreateTime();
					//清空积分
					check(validity, shopId, tenantId, createTime);
				}
			}
		}
		return SUCCESS;
	}

    /**
     * 清空积分
     * @param validity
     * @param shopId
     * @param tenantId
     * @param createTime
     * @throws ParseException
     */
	private void check(Integer validity, String shopId, String tenantId, LocalDateTime createTime)
			throws ParseException {
		String userId = "";
		BigDecimal integral = new BigDecimal(0);
		//积分过期时间
		LocalDateTime staleDatedTime = createTime.plusMonths(validity);
		//现在时间
		LocalDateTime currentTime = LocalDateTime.now();

		System.out.println(staleDatedTime + "积分过期时间");
		System.out.println(currentTime + "现在时间");
		//现在时间大于 积分过期时间
		if (!(currentTime.isBefore(staleDatedTime))) {
			try {
				List<Map<String, Object>> maps = integralDetailService.selectAllUser(shopId, tenantId);
				if (CollectionUtil.isNotEmpty(maps)) {
					for (Map<String, Object> map : maps) {
						//获取用户id 以及要清空的积分
						userId = map.get("userId").toString();
						System.out.println(userId);
						integral = (BigDecimal) (map.get("integral"));
						//2.生成清空积分明细
						integralDetailService.generateNowIntegralDetailService(userId, integral, shopId, tenantId);
					}
					//修改这个商铺的所有积分明细清空状态
					integralDetailService.updateAll(shopId, tenantId);

					integralShopMapper.updateCreateTime(shopId, tenantId, currentTime);
				}

			} catch (Exception e) {
				log.error("清空积分失败"+e.getMessage());
			}
		}
	}
}